<?php

namespace app\admin\model;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    

    







    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function course()
    {
        return $this->belongsTo('Course', 'course_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public static function paySuccess($order_sn, $amount)
    {
        $order = self::where('order_no', $order_sn)->find();
        if (!$order) {
            throw new \Exception('订单不存在');
        }
        if ($order['status'] == 'paid') {
            return true;
        }
        if ($order['price'] != $amount) {
            throw new \Exception('支付金额不正确');
        }
        $order->status = 'paid';
        $order->pay_time = time();
        $order->exit_time=time()+3600*24*365;
        $order->save();
        return true;
    }
}
